package com.greenet.companytemplate.manager.web;

import com.greenet.companytemplate.manager.entity.FileBean;
import com.greenet.companytemplate.manager.entity.Menu;
import com.greenet.companytemplate.manager.entity.TMenu;
import com.greenet.companytemplate.manager.service.FileService;
import com.greenet.companytemplate.manager.service.SecondMenuService;
import com.greenet.companytemplate.manager.service.ThirdMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/3/30.
 */
@Controller("/loginpage")
public class LoginController {
    protected Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private SecondMenuService secondMenuService;
    @Autowired
    private ThirdMenuService thirdMenuService;
    @Autowired
    private FileService fileService;
    /**
     * 跳转到登录页面
     *
     * @param error
     * @param logout
     * @param model
     * @return
     */
    /*@RequestMapping("/loginpage")
    public String login(@RequestParam(value = "error", required = false) String error,
                        @RequestParam(value = "logout", required = false) String logout,
                        Model model) {


        if (error != null) {
            model.addAttribute("error", "登录出错！");
        }
        if (logout != null) {
            model.addAttribute("logout", "成功退出！");
        }
        logger.info("loginning..........");
        return "login";
    }*/

    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,Model model,
                        @RequestParam(value = "pid") String pid,
                        @RequestParam(value = "type",required = false) String type
                        ) {
        System.out.println(pid+"*");
        if ("0".equals(pid)||pid==null){
            pid="1";
        }
        List<Menu> list = new ArrayList();
        List<Menu> list1 = new ArrayList();
        List<TMenu> tlist = new ArrayList();
        try {
            list = secondMenuService.getSubMenu(Integer.parseInt(pid));
            for(int i=0;i<list.size();i++){
                Menu menu=list.get(i);
                tlist = thirdMenuService.getTidMenu(Integer.parseInt(menu.getId()));
                menu.setTlist(tlist);
                list1.add(menu);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("list",list1);
        model.addAttribute("pid",pid);
        if(!"".equals(type) && type!=null){
            model.addAttribute("type",type);
            return "index";
        }
//        model.addAttribute("username", request.getUserPrincipal().getName());
        return "operator";
    }

    @RequestMapping("/index_welcome")
    public String welcome(Model model,@RequestParam(value = "type",required = false) String type){
        List<Menu> list = new ArrayList();
        List<TMenu> tlist = new ArrayList();
        List<Menu> list1 = new ArrayList();
        try {
            list = secondMenuService.getSubMenu(Integer.parseInt("1"));
            for(int i=0;i<list.size();i++){
                Menu menu=list.get(i);
                tlist = thirdMenuService.getTidMenu(Integer.parseInt(menu.getId()));
                menu.setTlist(tlist);
                list1.add(menu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("list",list1);
        model.addAttribute("pid",1);
        if(type!=null && !"".equals(type)){
            model.addAttribute("type",type);
            return "index";
        }

        return "operator";
    }
    /**
     * 快速搜索
     * @return
     */
    @RequestMapping("/info")
    public String info(@RequestParam(value = "name",required = false) String name,
                       Model model){
        FileBean fileBean=new FileBean();
        if(name!=null){
            fileBean.setName(name);
            model.addAttribute("name",name);
        }
        List<FileBean> list=fileService.findList(fileBean);
        model.addAttribute("list",list);
        return "index/info";
    }

    @RequestMapping("/home")
    public String home(Model model){
        FileBean fileBean=new FileBean();
        Date date=new Date();
        Calendar c=Calendar.getInstance();
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day-7);
        fileBean.setStartDate(c.getTime());
        fileBean.setEndDate(date);
        List<FileBean> list=fileService.getRecently(fileBean);
        List<FileBean> list1=new ArrayList<FileBean>();
        for(int i=0;i<list.size();i++){
            FileBean bean=list.get(i);
            String url=bean.getUrl();
            if(url!=null){
                String fileName=url.substring(url.lastIndexOf("/")+1,url.length());
                bean.setFileName(fileName);
                list1.add(bean);
            }
        }
        model.addAttribute("list",list1);
        return "index/home";
    }


}

